package com.dcoinpay.sys.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dcoinpay.common.utils.DataResult;
import com.dcoinpay.sys.entity.DictDetail;
import com.dcoinpay.sys.entity.SysDictEntity;
import com.dcoinpay.sys.service.SysDictDetailService;
import com.dcoinpay.sys.service.SysDictService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 字典管理
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("/sys/dict")
public class DictController {
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictDetailService sysDictDetailService;
    
    /**
    * 跳转到页面
    */
    @GetMapping("/listview")
    public ModelAndView listview() {
        return new ModelAndView("sys/dict/list");
    }


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @RequiresPermissions("sys:dict:add")
    public DataResult<SysDictEntity> add(@RequestBody SysDictEntity sysDict) {
        if (StringUtils.isEmpty(sysDict.getName())) {
            return DataResult.fail("字典名称不能为空");
        }
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<SysDictEntity>();
        queryWrapper.eq("name", sysDict.getName());
        SysDictEntity q = sysDictService.getOne(queryWrapper);
        if (q != null) {
            return DataResult.fail("字典名称已存在");
        }
        sysDictService.save(sysDict);
        return DataResult.success();
    }

	@ApiOperation(value = "删除")
    @DeleteMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public DataResult<SysDictEntity> delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        sysDictService.removeByIds(ids);
        //删除detail
        QueryWrapper<DictDetail> queryWrapper = new QueryWrapper<DictDetail>();
        queryWrapper.in("dict_id", ids);
        sysDictDetailService.remove(queryWrapper);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public DataResult<SysDictEntity> update(@RequestBody SysDictEntity sysDict) {
        if (StringUtils.isEmpty(sysDict.getName())) {
            return DataResult.fail("字典名称不能为空");
        }
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<SysDictEntity>();
        queryWrapper.eq("name", sysDict.getName());
        SysDictEntity q = sysDictService.getOne(queryWrapper);
        if (q != null && !q.getId().equals(sysDict.getId())) {
            return DataResult.fail("字典名称已存在");
        }

        sysDictService.updateById(sysDict);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public DataResult<Object> findListByPage(@RequestBody SysDictEntity sysDict) {
        Page<SysDictEntity> page = new Page<SysDictEntity>(sysDict.getPage(), sysDict.getLimit());
        QueryWrapper<SysDictEntity> queryWrapper = new QueryWrapper<SysDictEntity>();
        //查询条件示例
        if (!StringUtils.isEmpty(sysDict.getName())) {
            queryWrapper.like("name", sysDict.getName());
            queryWrapper.or();
            queryWrapper.like("remark", sysDict.getName());
        }
        queryWrapper.orderByAsc("name");
        IPage<SysDictEntity> iPage = sysDictService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }
    

}
